package org.steven.crm.core.service.user;

import com.steven.common.utils.DateTimeUtil;
import com.steven.common.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.steven.crm.core.auth.UserAuthHandler;
import org.steven.crm.core.auth.WechatUserAuthHandler;
import org.steven.crm.core.auth.dto.GrantTokenRequest;
import org.steven.crm.core.dto.user.BindRequest;
import org.steven.crm.core.dto.user.LoginRequest;
import org.steven.crm.core.exception.ServiceException;
import org.steven.crm.core.service.BaseService;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author wuzw
 * @Date 2024/1/10 17:07
 * @Version 1.0
 */
public class WechatUserService extends BaseService {
    private static UserService userService = new UserService();

    private static WechatUserAuthHandler wechatUserAuthHandler = new WechatUserAuthHandler();

    @Override
    protected String getTable() {
        return "wechatUser";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public Map<String, String> readUserIdByOpenId(String openId) throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("openId", openId);

        return DBManager.queryOne(getSession(), getTable(), k);
    }

    public Map<String, String> readByBindCode(String bindCode) throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("bindCode", bindCode);

        return DBManager.queryOne(getSession(), getTable(), k);
    }

    public Map<String, String> login(LoginRequest request) throws Exception {
        GrantTokenRequest grantTokenRequest = GrantTokenRequest.builder().build()
                .setClientId(request.getClientId())
                .setUsername(request.getMobile()).setCode(request.getCode());

        Map<String, String> token = wechatUserAuthHandler.grant(grantTokenRequest);

        if (StringUtils.isNotEmpty(token.get("userId"))) {
            Map<String, String> updateV = new HashMap<>();
            updateV.put("loginTime", DateTimeUtil.getCurrentDateTime());
            userService.update(token.get("userId"), updateV);
        }

        return token;
    }

    public void bindUser(BindRequest request) throws Exception {
        Map<String, String> data = readByBindCode(request.getBindCode());
        if (data.isEmpty()) {
            throw new ServiceException("无效的绑定验证码");
        }
        if (StringUtils.isNotEmpty(data.get("userId"))) {
            throw new ServiceException("当前已绑定了用户");
        }

        Map<String, String> user = userService.readByMobile(request.getMobile());
        if (user.isEmpty()) {
            throw new ServiceException("不存在该用户");
        }
        if (!MD5Util.MD5(request.getPassword()).equals(user.get("password"))) {
            throw new ServiceException("用户名或密码不正确");
        }

        Map<String, String> v = new HashMap<>();
        v.put("userId", user.get("id"));
        this.update(data.get("id"), v);
    }
}
